#!/usr/bin/python3

###############################################################
## Name       : shutcut_proc
## Author     : xiaotu
## Time       : 2022-09-16 05:01:55
## Description:
## 
## 
###############################################################

import sys
import os
import re
import argparse
import math

def input_args_proc():
    global cmp
    global vd
    global sg
    parser = argparse.ArgumentParser(description="argparse info")
    parser.add_argument('-o', action='store_true', default=False, help='open this script')
    parser.add_argument('-c', action='store_true', default=False, help='compile')
    parser.add_argument('-v', action='store_true', default=False, help='verdi')
    parser.add_argument('-s', action='store_true', default=False, help='spyglass')
    
    result = parser.parse_args()
    if result.o == True:
        os.system("gvim %s" % __file__)
        sys.exit(0)

    cmp = result.c
    vd  = result.v
    sg  = result.s
pass

def find_sim_list():
    sel_file = ""
    file_list = os.popen("ls | grep _sim.lst").readlines()
    
    if len(file_list) > 1:
        num = 0
        for line in file_list:
            print(str(num) + " - " + line.strip())
            num += 1
        sel = input("please sel a sim list: ")
        if int(sel) < int(num):
            sel_file = file_list[int(sel)].strip()
    elif len(file_list) == 1:
        sel_file = file_list[0].strip()
 
    if sel_file == "":
        sys.exit("NO SIM LIST FILE")

    module = re.sub(r"_sim\.lst", "", sel_file)
    
    return sel_file, module
pass

def auto_proc(list, top):
    proc = ""
    if cmp:
        proc = "cmp_rtl -f %s -t %s" % (list, top)
    elif vd:
        proc = "verdi -f %s -top %s -c &" % (list, top)
    elif sg:
        proc = "spyg_rtl -f %s -t %s" % (list, top)

    print("AUTO_PROC: " + proc)
    os.system(proc)
pass

def main():
    print("%s is working" % __file__)
    input_args_proc()
    (list, top) = find_sim_list()
    auto_proc(list, top)

if __name__ == "__main__":
    main()
